﻿
//页面状态
var State;
//控件信息集合
var ControlsCollection;
//当前明细的Keyid
var Keyid;
//明细对象
var itemObject;
$(document).ready(function () {
    var collection = [
        New(ASPxRFLabel, ['lblKeyid', ['GroupA'], 'Keyid']),
        New(ASPxRFLabel, ['lblHelpKeyid', ['GroupA'], 'HelpKeyid']),
        New(ASPxRFLabel, ['lblParentKeyid', ['GroupA'], 'ParentKeyid']),
        New(ASPxRFTextBox, ['txtPageName', ['GroupA'], 'PageName']),
        New(ASPxRFTextBox, ['txtHelpTitle', ['GroupA'], 'HelpTitle']),
        New(ASPxRFTextBox, ['txtPageCode', ['GroupA'], 'PageCode']),
        New(ASPxRFTextBox, ['txtPageNameEn', ['GroupA'], 'PageNameEn']),
        New(ASPxRFComboBox, ['cbModule', ['GroupA'], { valueField: 'ModuleKeyid'}]),
        New(ASPxRFTextBox, ['txtImageUrl', ['GroupA'], 'ImageUrl']),
        New(ASPxRFTextBox, ['txtNavigateUrl', ['GroupA'], 'NavigateUrl']),
        New(ASPxRFSpinEdit, ['seOrder', ['GroupA'], 'Order']),
        New(ASPxRFRadioButtonList, ['rblPageType', ['GroupA'], { valueField: 'PageType' }]),
        New(ASPxRFTextBox, ['txtRemark', ['GroupA'], 'Remark']),
        New(ASPxRFHyperLink, ['lnkhelp', ['GroupA'], '','']),
        New(ASPxRFRadioButtonList, ['rblIsAssignment', ['GroupA'], { valueField: 'IsAssignment' }, { valueFieldType: 'Boolean'}]),
        New(ASPxRFLabel, ['lblCreateName', ['GroupA'], 'CreateName']),
        New(ASPxRFLabel, ['lblCreateDate', ['GroupA'], 'CreateDate', 'Date']),
        New(ASPxRFLabel, ['lblModifyName', ['GroupA'], 'ModifyName']),
        New(ASPxRFLabel, ['lblModifyDate', ['GroupA'], 'ModifyDate', 'Date'])
       
    ];
    ControlsCollection = New(RapidForm, [collection]);

    processState($.query.get('State'));
});


//**********************************************//
//处理页面状态
var processState = function () {
    //设置页面状态
    State = arguments[0];
    if (State == 'Add') {
        //菜单按钮
        doSetEnableAndSelectMenuItem('btnAdd', 'btnAdd');
        //底部按钮
        txtPageName.Focus();
        doSetButtonBarVisibility(State);
        lblKeyid.SetText($.newGuid());

    } else if (State == 'View') {//添加-》查看； 编辑-》查看；
        if (arguments.length != 2) {
            throw new Error('方法参数有错');
        }
        doSetEnableMenuItems(true);
        doSetButtonBarVisibility(State);
        doBindItem(arguments[1]);
    } else if (State == 'Edit') {

        doBindItem($.query.get('Keyid'), true);
        doSetEnableAndSelectMenuItem('btnEdit', 'btnEdit');
        txtPageName.Focus();
        doSetButtonBarVisibility(State);
    } else if (State == 'Load') {
        doBindItem($.query.get('Keyid'));
    }
}

/**********************************/
//启用或者全部菜单按钮
var doSetEnableMenuItems = function (isEnable) {
    var count = menuHeader.GetItemCount();
    for (var i = 0; i < count; i++) {
        menuHeader.GetItem(i).SetEnabled(isEnable);
    }
}
//启用并选中指定菜单，禁用其他菜单
var doSetEnableAndSelectMenuItem = function (enableNames, selectedName) {
    var menuItemNames = enableNames.split(',');
    //禁用全部菜单项
    doSetEnableMenuItems(false);
    //启用菜单项
    for (var i = 0; i < menuItemNames.length; i++) {
        menuHeader.GetItemByName(menuItemNames[i]).SetEnabled(true);
    }
    //选中指定菜单项
    menuHeader.SetSelectedItem(menuHeader.GetItemByName(selectedName));
}

//重新绑定明细,绑定后是否可编辑 doBindItem(keyid)
var doBindItem = function (keyid, isEnable, afterBindItem) {
    var enable = false;
    if (arguments.length >= 2) {
        enable = arguments[1];
    }
    SGDemo.Permission.PageItem.GetUserByKeyid(keyid, function (result) {
        //重新绑定
        ControlsCollection.update(result.value, 'GroupA');
        //让表单元素是否编辑
        ControlsCollection.setEnabled(enable, 'GroupA');
        if (afterBindItem) {
            afterBindItem();
        }
    });
}

//底部操作按钮显示和隐藏
var doSetButtonBarVisibility = function (state) {
    switch (state) {
        case 'Add': $('.ui-btn-saveforadd').addClass('ui-btn-bottom-selected'); break;
        case 'Edit': $('.ui-btn-saveforedit').addClass('ui-btn-bottom-selected'); break;
        case 'View': $('.ui-btn-bottom-selected').removeClass('ui-btn-bottom-selected'); break;
        default: throw new Error('参数错误'); break;
    }
}
/*********** 新增操作 ************/
//新增
var doAdd = function () {
    if (State == 'Add') {
        $.messager.alert('信息提示', '您已经处于新增状态！', 'info');
        return;
    }

    //重置新增的表单元素,不需要重置lblKeyid控件
    ControlsCollection.reset('GroupA', 'lblKeyid,lblParentKeyid,lnkhelp');

    //让新增的表单元素可编辑
    ControlsCollection.setEnabled(true, 'GroupA');

    //处理新增状态界面
    processState('Add');
}

//新增的保存
var doSaveForAdd = function () {
    //先验证
    if (ASPxClientEdit.ValidateEditorsInContainerById('form1')) {
        var page = ControlsCollection.get('GroupA', 'lblCreateName,lblCreateDate,lblModifyName,lblModifyDate,rblIsAssignment');
        var isAssignment = rblIsAssignment.GetSelectedItem().text;
        SGDemo.Permission.PageItem.SaveForAdd(page, isAssignment, function (result) {
            if (result.value) {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增成功'
                });
                //处理页面状态
                processState('View', lblKeyid.GetText());
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增失败!'
                });

            }
        });
    }
}

//保存完继续新增
var doSaveAndContinueToAdd = function () {
    //先验证
    if (ASPxClientEdit.ValidateEditorsInContainerById('form1')) {
        var page = ControlsCollection.get('GroupA', 'rblIsAssignment');
        var isAssignment = rblIsAssignment.GetSelectedItem().text;
        SGDemo.Permission.PageItem.SaveForAdd(page, function (result) {
            if (result.value) {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增成功！请继续新增..'
                });
                //重置全部控件
                ControlsCollection.reset('GroupA', 'lblParentKeyid,lnkhelp');
                txtPageName.Focus();
                //获取新的Keyid
                lblKeyid.SetText($.newGuid());
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增失败！'
                });
            }
        });
    }
}

//新增的重置
var doResetForAdd = function () {
    //不需要重置lblKeyid控件
    ControlsCollection.reset('GroupA', 'lblKeyid,lblParentKeyid,lnkhelp');
    //清除验证信息
    ASPxClientEdit.ClearEditorsInContainerById('form1');
}

/*********** 编辑操作 ************/
//编辑
var doEdit = function () {
    if (State == 'Edit') {
        $.messager.alert('信息提示', '您已经处于编辑状态！', 'info');
        return;
    }
    processState('Edit', lblKeyid.GetText());
    //让编辑的表单元素可编辑
    ControlsCollection.setEnabled(true, 'GroupA');

}

//编辑的保存
var doSaveForEdit = function () {
    //先验证

    if (ASPxClientEdit.ValidateEditorsInContainerById('form1')) {
        var page = ControlsCollection.get('GroupA', 'rblIsAssignment');
        var isAssignment = rblIsAssignment.GetSelectedItem().text;
        SGDemo.Permission.PageItem.SaveForEdit(page, isAssignment, function (result) {
            if (result.value) {
                $.messager.show({
                    title: '信息提示',
                    msg: '编辑成功'
                });
                //处理页面状态
                processState('View', lblKeyid.GetText());
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '编辑失败'
                });
            }
        });
    }
}

//取消编辑
var doCancelForEdit = function () {
    processState('View', lblKeyid.GetText());
    //取消验证
    ASPxClientEdit.ClearEditorsInContainerById('form1');
}

/*********** 删除操作 ************/
//删除
var doDelete = function () {
    $.messager.confirm('确认提示', '确认删除吗?', function (result) {
        if (result) {

            SGDemo.Permission.PageItem.Delete(lblKeyid.GetText(), function (result) {

                if (result.value) {
                    $.messager.show({
                        title: '信息提示',
                        msg: '删除成功',
                        showType: 'slide'
                    });
                   // parent.navTab.closeCurrentTab();

                } else {
                    $.messager.show({
                        title: '信息提示',
                        msg: '删除失败',
                        showType: 'slide'
                    });
                }
            });
        }
    });
}



//选择帮助标题
var btnAddHelpClick = function (s,e) {
    Shadowbox.compeleteFn = 'doCompeleteSelect';
    Shadowbox.returnFields = 'Keyid;Title';
    Shadowbox.selectionMode = 'Single'; //Single,Multiple
    var url = '../../../Views/Permission/Popup/SelectHelpPopup.aspx?SelectionMode=' + Shadowbox.selectionMode;

    Shadowbox.open({
        content: url,
        player: 'iframe',
        title: '选择帮助标题',
        height: 600,
        width: 920
    });
}
//完成事件
var doCompeleteSelect = function (selectedValues) {
    if (selectedValues) {
        var a = [];
        var b = [];
        for (i = 0; i < selectedValues.length; i++) {
            a.push(selectedValues[i][0]);
            b.push(selectedValues[i][1]);

        }
        var keyid = a.join(';');
        var title = b.join(';');
        lblHelpKeyid.SetText(keyid);
        txtHelpTitle.SetText(title);
    } else {
        lblHelpKeyid.SetText('');
        txtHelpTitle.SetText('');
    }
}
//帮助
var doHelp = function () {
    window.open('../../../Help.aspx', 'Help'); // 打开一个标签页
}